import React from 'react'
// ===================================================================== global declare
const { $http, $Lazy, $fn } = window
// ===================================================================== Lazy
const Modal 		= $Lazy.state(()=>import('@antd/modal'))
const SubmitForm 	= $Lazy.state(()=>import('#cpt/form/submit-form'))
const message 		= $Lazy.file(()=>import('@antd/message'))
// ===================================================================== 修改登录密码
class Index extends React.Component{
	state = {
		
	}
	// 修改密码表单
	forms = [
		{ label: '旧密码',		name: 'old_password', 	type:'password',	required: true, full:true },
		{ label: '新密码',		name: 'new_password',	type:'password',	required: true, full:true },
		{ label: '确认密码',		name: 'con_password',	type:'password',	required: true, full:true },
	]
	
	open = () => this.modal.open({ formRef: this.formRef })
	
	render(){
		const { uuid, onClose, overtime } = this.props
		const { days, hours, minutes, seconds } = overtime || {}
		return (
			<Modal
				title		= '修改登录密码'
				width		= { 550 }
				loading 	= { this.state.submitLoading }
				onRef		= { ref => this.modal = ref }
			>
				{
					$fn.hasObject(overtime) && (
						<div className='f12 mb15' style={{color:'red'}}>
							<span className='mr5'>由于您已经超过</span>
							<b>{$fn.nan(days)}<span className='mlr5'>天</span>
							{$fn.nan(hours)}<span className='mlr5'>小时</span>
							{$fn.nan(minutes)}<span className='mlr5'>分钟</span>
							{$fn.nan(seconds)}<span className='mlr5'>秒</span></b>
							<span>未修改密码，按系统要求，您需要强制修改密码!!!</span>
						</div>
					)
				}
				<SubmitForm
					data = { this.forms }
					hideButton
					labelWidth = 's5'
					onSubmit = { param => {
						const { old_password, new_password, con_password} = param
						
						if(old_password === new_password){ return message.then(f=>f.error('旧密码与新密码相同'))	}
						if(con_password !== new_password){ return message.then(f=>f.error('新密码两次输入密码不一致')) }
						
						if(!$fn.isPwd(new_password) && !$fn.isPwd(con_password)){ return message.then(f=>f.error('新密码为 6 ~ 18 位的字符'))	}
						
						$http.submit(this,'auth/updatePwd', { param:{ ...param, uuid } }).then(data=>{
							this.modal.close()
							
							message.then(f=>f.success('修改密码成功'))
							
							if(!$fn.hasObject(overtime)){ $fn.go('/login?mod=1') }
							
							onClose && onClose()
						})
					}}
					onRef = { ref => this.formRef = ref }
				 />
			</Modal>
		)
	}
}

export default Index